const fs = require('fs')
const path = require('path')
const { weatherApi, HWeatherApi } = require('../api/index');
const newsControllers = require('../services/index');
var LOCATION = "ip"; // 除拼音外，还可以使用 v3 id、汉语等形式
var argv = require('optimist').default('l', LOCATION).argv;
module.exports = {
    async getHomePage (ctx) {
        argv.location = ctx.query.location || '深圳'
        await weatherApi.getWeatherNow(argv).then(function (data) {
            // ctx.state.weather = data.results[0]
            // ctx.render('./views/index.vue')
            ctx.render({
                path: path.resolve(__dirname, '../views/pages/index.vue'),
                components: {
                    Layout: path.resolve(__dirname, '../views/layout/index.vue'),
                    Tabs: {
                        path: path.resolve(__dirname, '../views/layout/tabs.vue'),
                        data () {
                            return {
                                data: {
                                    tabs: newsControllers.tabs,
                                },
                                query: { ...ctx.query || {}, url: ctx.url }
                            }
                        },
                    }
                },
                data () {
                    return {
                        weathers: data.results,
                        tabs: newsControllers.tabs
                    }
                },
                methods: {
                    async addCity () {
                        let locations = ['上海', '北京', '厦门', '赣州', '深圳']
                        let res = await weatherApi.getWeatherNow({ location: locations[Math.floor(Math.random() * locations.length - 1)] })
                        this.weathers.push(res.results[0])
                    }
                }
            })
        }).catch(function (err) {
            console.log(err, 'error');
            // ctx.state.weather = err
        })
    },
    async template (ctx) {
        await ctx.render('tpl', {
            title: "home"
        })
    }
}